package com.wn.service.impl;

import com.wn.bean.Student;
import com.wn.mapper.StuMapper;
import com.wn.service.StudentService;
import com.wn.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.Servlet;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2022 -06-30
 * @desc
 */
public class StudentServiceImpl implements StudentService {

    @Override
    public Student findStuById(int id) {

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        Student stu = mapper.findStuById(id);

        return stu;
    }

    @Override
    public boolean update(Student student) {

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);

       int row = mapper.update(student);
        sqlSession.close();

        return row > 0 ;
    }

    @Override
    public List<Student> queryStudents() {

        //调用mapper
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);

        List<Student> stus = mapper.findStus();
        sqlSession.close();

        return stus;
    }

    @Override
    public boolean delStu(int id) {

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        int row = mapper.delStu(id);

        return row > 0;
    }

    @Override
    public boolean save(Student student) {

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StuMapper mapper = sqlSession.getMapper(StuMapper.class);
        int row = mapper.insertStu(student);
        return row > 0 ;
    }
}
